package org.hellojboss.simpledemo.dao.impl;

import javax.persistence.EntityManager;

import org.hellojboss.core.persistence.EntityManagerHelper;
import org.hellojboss.simpledemo.dao.UserDAO;
import org.hellojboss.simpledemo.model.User;

public class UserDAOImpl implements UserDAO {

	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}

	public void delete(User entity) {
		getEntityManager().remove(entity);
	}

	public User get(Integer id) {
		return getEntityManager().find(User.class, id);
	}

	public void save(User entity) {
		getEntityManager().persist(entity);
	}

	public void update(User entity) {
		getEntityManager().merge(entity);
	}

	@Override
	public User getByName(String loginName) {
		return (User) getEntityManager().createNativeQuery("SELECT u.* FROM user AS u WHERE u.loginname = ?", User.class).setParameter(1,
				loginName).getSingleResult();
	}
}